package com.boyadegroot.divelog.models;

import android.os.Parcel;
import android.os.Parcelable;
import android.support.annotation.NonNull;

public class Diver implements Parcelable, Comparable<Diver> {

    private static final String TAG = Diver.class.getName();

    private long mId;
    private String mUsername;
    private String mFirstName;
    private String mLastName;
    private long mBirthDate;
    private DiveCertification mCertification;

    public Diver() {}

    public Diver(long id, String username, String firstname, String lastname, long birthdate, DiveCertification certification) {
        this.setId(id);
        this.setUsername(username);
        this.setFirstName(firstname);
        this.setLastName(lastname);
        this.setBirthDate(birthdate);
        this.setCertification(certification);
    }

    public long getId() {
        return mId;
    }

    public void setId(long id) {
        this.mId = id;
    }

    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String username) {
        this.mUsername = username;
    }

    public String getFirstName() {
        return mFirstName;
    }

    public void setFirstName(String firstname) {
        this.mFirstName = firstname;
    }

    public String getLastName() {
        return mLastName;
    }

    public void setLastName(String lastname) {
        this.mLastName = lastname;
    }

    public String getFullName() {
        return String.format("%s %s", mFirstName, mLastName);
    }

    public long getBirthDate() {
        return mBirthDate;
    }

    public void setBirthDate(long birthdate) {
        this.mBirthDate = birthdate;
    }

    public DiveCertification getCertification() {
        return mCertification;
    }
    
    public void setCertification(DiveCertification certification) {
        this.mCertification = certification;
    }

    public String getCertificationName() {
        if (mCertification == null) {
            return null;
        }
        return mCertification.getFullName();
    }

    @Override
    public int compareTo(@NonNull Diver another) {
        return this.mUsername.compareTo(another.mUsername);
    }

    @Override
    public String toString() {
        return getUsername();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(mId);
        dest.writeString(mUsername);
        dest.writeString(mFirstName);
        dest.writeString(mLastName);
        dest.writeLong(mBirthDate);
        dest.writeParcelable(mCertification, flags);
    }

    public static final Parcelable.Creator<Diver> CREATOR = new Parcelable.Creator<Diver>() {

        public Diver createFromParcel(Parcel in) {
            return new Diver(in);
        }

        public Diver[] newArray(int size) {
            return new Diver[size];
        }
    };

    private Diver(Parcel in) {
        mId = in.readLong();
        mUsername = in.readString();
        mFirstName = in.readString();
        mLastName = in.readString();
        mBirthDate = in.readLong();
        mCertification = in.readParcelable(DiveCertification.class.getClassLoader());
    }
}
